package org.cwgl.interfaces.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.cwgl.dao.ClimasDao;
import org.cwgl.dao.StandbookDao;
import org.cwgl.dao.UsercaseDao;
import org.cwgl.exception.MyException;
import org.cwgl.interfaces.SelectService;
import org.cwgl.model.Climas;
import org.cwgl.model.Standbook;
import org.cwgl.model.Usercase;
import org.cwgl.support.BaseVo;
import org.cwgl.support.Page;

public class SelectSerImpl implements SelectService {
	private UsercaseDao usercaseDao;
	private StandbookDao standbookDao;
	private ClimasDao  climasDao;


	public void setUsercaseDao(UsercaseDao usercaseDao) {
		this.usercaseDao = usercaseDao;
	}

	public void setStandbookDao(StandbookDao standbookDao) {
		this.standbookDao = standbookDao;
	}

	public void setClimasDao(ClimasDao climasDao) {
		this.climasDao = climasDao;
	}
	/*
	 * (non-Javadoc)  JUnit测试 OK  2012-12-18
	 * @see org.cwgl.interfaces.SelectService#findUsercaseById(java.lang.Integer)
	 */
	@Override
	public Usercase findUsercaseById(Integer id) throws MyException {
		if(id<0 || id==null)
		{
			throw new MyException("id不正确,无法查询您要的结果!");
		}
		
		Usercase usercase=usercaseDao.get(Usercase.class,id);
		if(usercase==null)
		{
			throw new MyException("不存在此案例!");
		}
		if(usercase.getIsflag().equals("0"))
		{
			throw new MyException("无效的案例数据!");
		}
		return (usercase);
	}
	/*
	 * (non-Javadoc)  JUnit测试 OK  2012-12-18
	 * @see org.cwgl.interfaces.SelectService#findClimasById(java.lang.Integer)
	 */
	@Override
	public Climas findClimasById(Integer id) throws MyException {
		if(id<0 || id==null)
		{
			throw new MyException("id不正确,无法查询您要的结果!");
		}
		Climas climas=climasDao.get(Climas.class,id);
		if(climas==null)
		{
			throw new MyException("没有找到相关的帐务信息!");
		}
		if(climas.getIsflag().equals("0"))
		{
			throw new MyException("无效的帐务信息!");
		}
		return (climas);
	}
	/*
	 * (non-Javadoc)   JUnit测试 OK  2012-12-18
	 * @see org.cwgl.interfaces.SelectService#findStandbookById(java.lang.Integer)
	 */
	@Override
	public Standbook findStandbookById(Integer id) throws MyException {
		if(id<0 || id==null)
		{
			throw new MyException("id不正确,无法查询您要的结果!");
		}
		Standbook standbook=standbookDao.get(Standbook.class,id);
		if(standbook==null)
		{
			throw new MyException("没有找到相关的案例信息!");
		}
		if(standbook.getIsflag().equals("0"))
		{
			throw new MyException("无效的案例信息!");
		}
		return (standbook);
	}
	/*
	 * (non-Javadoc)   JUnit测试OK   2012-12-18
	 * @see org.cwgl.interfaces.SelectService#findStandbookByUsercaseId(java.lang.Integer, int, int)
	 */
	@Override
	public Page findStandbookByUsercaseId(Integer id, int start, int limit)
			throws MyException {
		if(id<0 || id==null)
		{
			throw new MyException("id不正确,无法查询您要的结果!");
		}
		if(start<0 || limit<0)
		{
			throw new MyException("请输入正确的查询数据格式");
		}
		BaseVo bv=new BaseVo();
		bv.setStart(start);
		bv.setLimit(limit);
		String sql="SELECT * FROM STANDBOOK WHERE UCID=?";
		
		Page page=standbookDao.findPageBySQL(bv, sql, id.toString());
		if(page==null)
		{
			throw new MyException("没有找到相关的案例信息!");
		}
		List<HashMap<String,Object>> standbooks=page.getResult();
		if(standbooks==null || standbooks.size()==0)
		{
			throw new MyException("没有找到相关的案例信息!");
		}
		for (int i = 0; i < standbooks.size(); i++) 
		{
			if(standbooks.get(i).get("ISFLAG").equals("0"))
			{
				standbooks.remove(i);
			}
		}
		return (page);
	}
	/**
	 *  JUnit测试OK   2012-12-19
	 * startTime, endTime指的是帐务的申报时间
	 */
	@Override
	public Page findClaimsByTime(Date startTime, Date endTime, int start,
			int limit) throws MyException {
		if(startTime==null)
		{
			throw new MyException("查询的开始时间不能为空!");
		}
		if(endTime==null)
		{
			throw new MyException("查询的结束时间不能为空!");
		}
		if(endTime.getTime() < startTime.getTime())
		{
			throw new MyException("您输入的开始日期比结束日期大!");
		}
		if(start<0 || limit<0)
		{
			throw new MyException("请输入正确的查询数据格式");
		}
		BaseVo bv=new BaseVo();
		bv.setStart(start);
		bv.setLimit(limit);
		String sql="SELECT * FROM CLIMAS WHERE CLOSINGDATE " +
				"BETWEEN TO_DATE(?,'yyyy-mm-dd') AND TO_DATE(?,'yyyy-mm-dd')";
		Page page=climasDao.findPageBySQL(bv, sql,
		new SimpleDateFormat("yyyy-MM-dd").format(startTime),
		new SimpleDateFormat("yyyy-MM-dd").format(endTime));
		
		if(page==null)
		{
			throw new MyException("没有找到相关的帐务信息!");
		}
		List<HashMap<String,String>> climas=page.getResult();
		if(climas==null || climas.size()==0)
		{
			throw new MyException("没有找到相关的帐务信息!");
		}
		for (int i = 0; i < climas.size(); i++) 
		{
			if(climas.get(i).get("ISFLAG").equals("0"))
			{
				climas.remove(i);
			}
		}
		return (page);
	}


	@Override
	public Page findStandbookByTime(Date startTime, Date endTime, int start,
			int limit) throws MyException {
		if(startTime==null)
		{
			throw new MyException("查询的开始时间不能为空!");
		}
		if(endTime==null)
		{
			throw new MyException("查询的结束时间不能为空!");
		}
		if(endTime.getTime() < startTime.getTime())
		{
			throw new MyException("您输入的开始日期比结束日期大!");
		}
		if(start<0 || limit<0)
		{
			throw new MyException("请输入正确的查询数据格式");
		}
		BaseVo bv=new BaseVo();
		bv.setStart(start);
		bv.setLimit(limit);
		String sql="SELECT * FROM STANDBOOK WHERE RECORDDATA " +
				"BETWEEN TO_DATE(?,'yyyy-mm-dd') AND TO_DATE(?,'yyyy-mm-dd')";
		Page page=standbookDao.findPageBySQL(bv, sql,
		new SimpleDateFormat("yyyy-MM-dd").format(startTime),
		new SimpleDateFormat("yyyy-MM-dd").format(endTime));
		
		if(page==null)
		{
			throw new MyException("没有找到相关的案例信息!");
		}
		List<HashMap<String,Object>> standbooks=page.getResult();
		if(standbooks==null || standbooks.size()==0)
		{
			throw new MyException("没有找到相关的案例信息!");
		}
		for (int i = 0; i < standbooks.size(); i++) 
		{
			if(standbooks.get(i).get("ISFLAG").equals("0"))
			{
				standbooks.remove(i);
			}
		}
		return (page);
	}

	@Override
	public Double findUsercaseMaoriByTime(Date startTime, Date endTime)
			throws MyException {
		Double maor=0.0;

		Page page=findStandbookByTime(startTime, endTime, 0, 999999);
		
		List<HashMap<String,Object>> standbooks=page.getResult();

		
		Double sal=0.0;//销售金额
		Double rate=0.0;//税率
		Double crossmoney=0.0;//带税毛利
	
		for (int i = 0; i < standbooks.size(); i++) 
		{
			if(standbooks.get(i).get("ISFLAG").equals("0"))
			{
				standbooks.remove(i);
			} 
				BigDecimal ucid=(BigDecimal)standbooks.get(i).get("UCID");
			
				Integer longi=ucid.intValue();
				Page p=findStandbookByUsercaseId(longi, 0, 9999999);
//				Usercase usercase=findUsercaseById(longi);
			
//				crossmoney=usercase.getGrossmoney();//找到带税利润
			
			
			String mon;
			int inc=0;//成本费用
			for(int j=0; j<p.getResult().size(); j++)
			{
				mon=(String)standbooks.get(j).get("MONEYTYPE").toString();
				if(mon.equals("0"))
				{
					
					BigDecimal binc=(BigDecimal)standbooks.get(j).get("INCOMEMONEY");

					inc +=binc.intValue();
//					System.out.println(inc);
					
				}
				if(mon.equals("1"))
				{
					BigDecimal bsal=(BigDecimal)standbooks.get(i).get("SALESMONEY");
				    sal=bsal.doubleValue();
				}
				crossmoney=sal-inc;
			}
			
			System.out.println("cro:"+crossmoney);
			mon=(String)standbooks.get(i).get("MONEYTYPE").toString();
			if(mon.equals("1"))
			{
//				BigDecimal bsal=(BigDecimal)standbooks.get(i).get("SALESMONEY");
//			    sal=bsal.doubleValue();
				BigDecimal brate=(BigDecimal)standbooks.get(i).get("RATE");
				rate=brate.doubleValue();
				maor +=crossmoney*(1-rate);
				System.out.println(maor);
			}
		}
		
		
		
		return maor;
	}

	@Override
	public Double findClimasMaoriByTime(Date startTime, Date endTime)
			throws MyException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page findAllUsercase() throws MyException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page findAllClimas() throws MyException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Double findRealMaori(Date startTime, Date endTime)
			throws MyException {
		// TODO Auto-generated method stub
		return null;
	}

	

}
